package org.company.management.controller;

import org.company.management.ui.EditorInput;
import org.company.management.ui.MainEditor;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * @author Paner
 *
 */
public class UiController
{

	public static void showEditor (String editorTitle, String editorId)
	{
		try
		{
			closeOpened();

			try {
				IWorkbenchPage _page = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage(); 
				_page.openEditor(new EditorInput(editorTitle), editorId);
			} catch (PartInitException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public static void showEditor(Object input, String editorId)
	{
		try
		{
			closeOpened();
			IWorkbenchPage _page = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage(); 
			
		  
			try {
				_page.openEditor(new EditorInput(input), editorId);
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
		
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public static void showView(String viewId)
	{
		
		try
		{
			IWorkbenchPage _page = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage();
			_page.setEditorAreaVisible(false);

			_page.showView(viewId);
		}
		catch (PartInitException e)
		{
			e.printStackTrace();
		}
	}
	
	private static void closeOpened()
	{
		try
		{
			IWorkbenchPage _page = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage();
			_page.closeAllEditors(true);
			IViewPart view = _page.findView(MainEditor.ID);
	
			if (view != null) {
				_page.hideView(view); 
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
}
